package com.buaa.divinationmaster.core.database.entities;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;

/**
 * 六十四卦数据实体类
 * 包含64卦的完整信息，用于梅花易数和金钱卦占卜
 */
@Entity(tableName = "hexagram_64")
public class Hexagram {
    
    @PrimaryKey
    public int id;                    // 1-64 (卦序)
    
    @ColumnInfo(name = "name")
    public String name;               // 卦名 (如：乾、坤、屯、蒙等)
    
    @ColumnInfo(name = "upper_trigram")
    public String upperTrigram;       // 上卦 (乾、兑、离、震、巽、坎、艮、坤)
    
    @ColumnInfo(name = "lower_trigram") 
    public String lowerTrigram;       // 下卦
    
    @ColumnInfo(name = "symbol")
    public String symbol;             // 卦象符号 (如：☰☰ 表示乾卦)
    
    @ColumnInfo(name = "interpretation")
    public String interpretation;     // 卦辞
    
    @ColumnInfo(name = "element")
    public String element;            // 五行属性
    
    @ColumnInfo(name = "keywords")
    public String keywords;           // 关键词 (逗号分隔)
    
    @ColumnInfo(name = "judgment")
    public String judgment;           // 彖辞
    
    @ColumnInfo(name = "image")
    public String image;              // 象辞
    
    @ColumnInfo(name = "direction")
    public String direction;          // 方位属性
    
    @ColumnInfo(name = "meaning")
    public String meaning;            // 卦象含义
    
    // 默认构造函数
    public Hexagram() {}
    
    // 带参构造函数 - Room忽略此构造函数
    @Ignore
    public Hexagram(int id, String name, String upperTrigram, String lowerTrigram) {
        this.id = id;
        this.name = name;
        this.upperTrigram = upperTrigram;
        this.lowerTrigram = lowerTrigram;
    }
    
    // Getter和Setter方法
    public int getId() { return id; }
    public void setId(int id) { this.id = id; }
    
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    
    public String getUpperTrigram() { return upperTrigram; }
    public void setUpperTrigram(String upperTrigram) { this.upperTrigram = upperTrigram; }
    
    public String getLowerTrigram() { return lowerTrigram; }
    public void setLowerTrigram(String lowerTrigram) { this.lowerTrigram = lowerTrigram; }
    
    public String getSymbol() { return symbol; }
    public void setSymbol(String symbol) { this.symbol = symbol; }
    
    public String getInterpretation() { return interpretation; }
    public void setInterpretation(String interpretation) { this.interpretation = interpretation; }
    
    public String getElement() { return element; }
    public void setElement(String element) { this.element = element; }
    
    public String getKeywords() { return keywords; }
    public void setKeywords(String keywords) { this.keywords = keywords; }
    
    public String getJudgment() { return judgment; }
    public void setJudgment(String judgment) { this.judgment = judgment; }
    
    public String getImage() { return image; }
    public void setImage(String image) { this.image = image; }
    
    public String getDirection() { return direction; }
    public void setDirection(String direction) { this.direction = direction; }
    
    public String getMeaning() { return meaning; }
    public void setMeaning(String meaning) { this.meaning = meaning; }
    
    /**
     * 获取完整卦象符号 (上卦+下卦)
     */
    public String getFullSymbol() {
        if (symbol != null && !symbol.isEmpty()) {
            return symbol;
        }
        // 如果没有预设符号，根据上下卦组合生成
        return getTrigramSymbol(upperTrigram) + getTrigramSymbol(lowerTrigram);
    }
    
    /**
     * 根据三卦名称获取对应符号
     */
    private String getTrigramSymbol(String trigramName) {
        if (trigramName == null) return "☰";
        switch (trigramName) {
            case "乾": return "☰";
            case "兑": return "☱";
            case "离": return "☲";
            case "震": return "☳";
            case "巽": return "☴";
            case "坎": return "☵";
            case "艮": return "☶";
            case "坤": return "☷";
            default: return "☰";
        }
    }
    
    @Override
    public String toString() {
        return "Hexagram{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", upperTrigram='" + upperTrigram + '\'' +
                ", lowerTrigram='" + lowerTrigram + '\'' +
                ", symbol='" + symbol + '\'' +
                '}';
    }
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        Hexagram hexagram = (Hexagram) obj;
        return id == hexagram.id;
    }
    
    @Override
    public int hashCode() {
        return Integer.hashCode(id);
    }
}
